<?php
/* -=-=-=-=-=-=-=-=-=-=-=-=-
     author by terry
-=-=-=-=-=-=-=-=-=-=-=-=- */
error_reporting(7);
require "global.php";
cpheader();
//set offset

if(!empty($_POST[offset]))
{$offset = $_POST[offset];}
else if  (!empty($_GET[offset]))
{$offset = $_GET[offset]; }
else  {$offset = 0; }


if ($_GET[action]=="add"||$_GET[action]=='mod'||$_GET[action]=='view'){

    $cpforms->inithtmlarea();
	if($_GET[action]=="add"){
			$cpforms->formheader(array('title'=>'Add New Training (<font color=red>*</font>=Required Field)',
										'name'=>'article',
			                             'enctype'=>'multipart/form-data'));

			$cpforms->makehidden(array('name'=>'action',
									   'value'=>'doinsert'));
			$rs[StartDate] = $rs[EndDate] = time();
	}
	if($_GET[action] == "mod"){
			$cpforms->formheader(array('title'=>'Modify Training (<font color=red>*</font>=Required Field)',
										'name'=>'article',
				                        'enctype'=>'multipart/form-data'));

			$cpforms->makehidden(array('name'=>'action',
									   'value'=>'update'));
		    $trainingid = intval($_GET[id]);
			$cpforms->makehidden(array('name'=>'id',
									   'value'=>$trainingid));
			$cpforms->makehidden(array('name'=>'offset',
									   'value'=>$offset));
            $read =1;
	        $rs  = $DB->fetch_one_array("SELECT * FROM ".$db_prefix."training WHERE tr_id =".$trainingid);
	}

	if($_GET[action]=="view"){
				$cpforms->formheader(array('title'=>'View Training (<font color=red>*</font>=Required Field)',
										'name'=>'article',
				                        'enctype'=>'multipart/form-data'));
		    $trainingid = intval($_GET[id]);
			$cpforms->makehidden(array('name'=>'id',
									   'value'=>$trainingid));
			$cpforms->makehidden(array('name'=>'offset',
									   'value'=>$offset));
            $view =1;
	        $rs  = $DB->fetch_one_array("SELECT * FROM ".$db_prefix."training WHERE tr_id =".$trainingid);
	}

	
	?>
     <?      
		  echo ' <link rel="stylesheet" type="text/css" media="all" href="../jscalendar/calendar-win2k-cold-1.css" title="win2k-cold-1" />
               <script type="text/javascript" src="../jscalendar/calendar.js"></script>
               <script type="text/javascript" src="../jscalendar/lang/calendar-en.js"></script>
               <script type="text/javascript" src="../jscalendar/calendar-setup.js"></script>';
		   $cpforms->gethightext(array('text'=>"Topic",'name'=>"topic",'only'=>1,'value'=>"$rs[tr_topic]",'view'=>"$view"));
		   //$cpforms->gethightext(array('text'=>"Date",'name'=>"date",'only'=>1,'value'=>"$rs[tr_date]",'view'=>"$view"));
	       echo '<tr>
								<td width="30%"><strong>Date:</strong></td>
								<td>
									<input  type="text" name="date" size="25" id="f_date_c" readonly value="'.date('m/d/Y',strtotime($rs[tr_date])).'"><img src="images/img.gif" id="f_trigger_c" style="cursor: pointer; border: 0px solid red;" align="absmiddle" title="Date selector" />
									<script type="text/javascript">
									Calendar.setup({
										inputField     :    "f_date_c",     // id of the input field
										ifFormat       :    "%m/%d/%Y",      // format of the input field
										button         :    "f_trigger_c",  // trigger for the calendar (button ID)
										align          :    "Tl",           // alignment (defaults to "Bl")
										singleClick    :    true
									});
									</script>
								</td>
							</tr>';
		   $cpforms->getdoubletext(array('text'=>"Time(Indicate Timezone)",'only'=>1,'value1'=>"$rs[tr_time]",'value2'=>"$rs[tr_timezone]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Skype ID", 'name'=>"skypeid",'value'=>"$rs[tr_skype]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Host Name", 'name'=>"name",'only'=>1,'value'=>"$rs[tr_name]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Company Name", 'name'=>"companyname",'value'=>"$rs[tr_copname]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Email",'name'=>"email",'only'=>1,'value'=>"$rs[tr_email]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Phone", 'name'=>"phone",'value'=>"$rs[tr_phone]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Address",'name'=>"address",'value'=>"$rs[tr_address]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Location(City,State)",'name'=>"location",'value'=>"$rs[tr_location]",'view'=>"$view"));
		   $cpforms->gethightext(array('text'=>"Register Link",'name'=>"registerlink",'value'=>"$rs[tr_registerlink]",'view'=>"$view"));          
		   $cpforms->maketextarea(array('text'=>"Topic/Subject Description",'name'=>"description",'value'=>"$rs[tr_description]",'view'=>"$view"));
		   $cpforms->getbox(array('text'=>"Show as Top 3",'name'=>"showtop",'value'=>"$rs[tr_showtop]",'view'=>"$view"));
     ?>

<?
		
      if($_GET[action]=="view"){$cpforms->noformfooter();}
	   else{ $cpforms->formfooter(array('confirm'=>1));}
}


if ($_GET[action]=="list"){


	if($_GET[action]=="list"){   
		$nav = new buildNav;

		$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM ".$db_prefix."training");

		$nav->total_result = $total[count];
 
		if (empty($total[count])) {
			pa_exit("<font color=red>No training!</font><input type=button value='Add New' onclick='location.href=\"training.php?action=add\"'>");
		}
		$by = "tr_id"; 
		$order = "DESC";
        $_GET[order1]==''&&$order1=1;$_GET[order2]==''&&$order2=1;$_GET[order3]==''&&$order3=1;$_GET[order4]==''&&$order4=1;
		switch($_GET[swh]){
		   case 1:
			   $by = "tr_date";
               if($_GET[order1]==1){$order1=2; $order=" DESC ";
			   $image1 = "<img src=\"images/dot1.gif\">";}else{$order1=1;$order=" ASC ";
			   $image1 = "<img src=\"images/dot2.gif\">";}
			   break;
           case 2:
               $by = "tr_topic";
               if($_GET[order2]==1){$order2=2; $order=" DESC ";$image2 = "<img src=\"images/dot1.gif\">";}else{$order2=1;$order=" ASC ";$image2 = "<img src=\"images/dot2.gif\">";}
			   break;
		   case 3:
                $by = "tr_copname";
               if($_GET[order3]==1){$order3=2; $order=" DESC ";$image3 = "<img src=\"images/dot1.gif\">";}else{$order3=1;$order=" ASC ";$image3 = "<img src=\"images/dot2.gif\">";}
			   break;
		   case 4:
               $by = "tr_name";
               if($_GET[order4]==1){$order4=2; $order=" DESC ";$image4 = "<img src=\"images/dot1.gif\">";}else{$order4=1;$order=" ASC ";$image4 = "<img src=\"images/dot2.gif\">";}
			   break;	
		    case 5:
                $by = "tr_location";
               if($_GET[order5]==1){$order5=2; $order=" DESC ";$image5 = "<img src=\"images/dot1.gif\">";}else{$order5=1;$order=" ASC ";$image5 = "<img src=\"images/dot2.gif\">";}
			   break;	
			 case 6:
                $by = "tr_showtop";
               if($_GET[order6]==1){$order6=2; $order=" DESC ";$image6 = "<img src=\"images/dot1.gif\">";}else{$order6=1;$order=" ASC ";$image6 = "<img src=\"images/dot2.gif\">";}
			   break;

		}
		
		$order = " ORDER BY $by $order";
		$nav->execute("SELECT * FROM ".$db_prefix."training $order");

    }
	
	echo $nav->title("Training list");
    echo $nav->pagenav();


    echo "<table border=\"0\" width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableoutline\">
	<script type=\"text/javascript\" src=\"http://download.skype.com/share/skypebuttons/js/skypeCheck.js\"></script>
	           <tr><td colspan=7><input type=button value=\"Add New Training\" onclick=\"location.href='training.php?action=add'\"></td></tr>
               <tr align=\"center\" class=\"tbhead\">
                <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order1=$order1&swh=1\">$image1 Date </a></td>
                <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order2=$order2&swh=2\">$image2 Topic </a></td>
                <td nowrap  width=\"10%\"> Time </td>
				 <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order3=$order3&swh=3\">$image3 Company </a></td>
				 <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order4=$order4&swh=4\">$image4 Host Name </a></td>
				 <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order5=$order5&swh=5\">$image5 Location </a></td>
				  <td nowrap  width=\"10%\"><a href=\"training.php?action=list&offset=$_GET[offset]&order6=$order6&swh=6\">$image6 Top3 </a></td>
				 <td nowrap  width=\"10%\"> Register Link </td>
				 <td nowrap  width=\"10%\"> Action </td>
               </tr>\n";
    while ($list = $DB->fetch_array($nav->sql_result)){
		   $top3 = ($list[tr_showtop]==1)?" <font color=red>Yes</font> ":""; 
           echo "<tr class=".getrowbg().">
                      <td align=\"left\" nowrap>".@date('m/d/Y',strtotime($list[tr_date]))."</td>
					  <td align=\"left\" nowrap>".$list[tr_topic]."</td>
					  <td align=\"left\" nowrap>".$list[tr_time]."</td>
                      <td align=\"left\" >".$list[tr_copname]."</td>
                      <td align=\"left\" >".$list[tr_name]."</td>
					  <td align=\"left\" >".$list[tr_location]."</td>
					  <td align=\"left\" >".$top3."</td>
					  <td align=\"left\" >".$list[tr_registerlink]."</td>
                      <td align=\"left\" nowrap><a href=\"training.php?action=view&offset=$_GET[offset]&id=$list[tr_id]\">View</a> | <a href=\"training.php?action=mod&offset=$_GET[offset]&id=$list[tr_id]\">Edit</a> | <a href=\"training.php?action=kill&offset=$_GET[offset]&id=$list[tr_id]\">Delete</a>";
    }
	if(empty($total[count])){
			echo "<tr><td><font color=red>No results!</font></td></tr>";
   }
    echo "</table>\n";
    echo $nav->pagenav();
}

if ($_GET[action]=="kill"){
    $cpforms->formheader(array('title'=>'Sure to delete this?'));
    $cpforms->makehidden(array('name'=>'action',
                                'value'=>'remove'));
    $cpforms->makehidden(array('name'=>'trainingid',
                                'value'=>$_GET[id]));
	$cpforms->makehidden(array('name'=>'offset',
                                'value'=>$_GET[offset]));
    $cpforms->formfooter(array('confirm'=>2));
}


if( $_POST[action]=="remove"){
    $DB->query("DELETE FROM ".$db_prefix."training WHERE tr_id='$_POST[trainingid]'");
    redirect("./training.php?action=list&offset=".$offset,"<font color=red>Deleted Successfully!</font>");
}



if ($_POST[action]=="doinsert"){

	$topic          = htmlspecialchars(trim($_POST[topic]));
	$date           = htmlspecialchars(trim($_POST['date']));
	$time           = htmlspecialchars(trim($_POST['time']));
	$timezone       = htmlspecialchars(trim($_POST[timezone]));
	$email          = htmlspecialchars(trim($_POST[email]));
	$skypeid        = htmlspecialchars(trim($_POST[skypeid]));
	$name           = htmlspecialchars(trim($_POST[name]));
	$companyname    = htmlspecialchars(trim($_POST[companyname]));
	$phone          = htmlspecialchars(trim($_POST[phone]));
	$registerlink   = htmlspecialchars(trim($_POST[registerlink]));
	$address        = htmlspecialchars(trim($_POST[address]));
	$location       = htmlspecialchars(trim($_POST[location]));
	$description    = ucfirst(htmlspecialchars(trim($_POST[description])));
	$showtop        = trim($_POST['showtop']);
   
	$sql = "INSERT INTO ".$db_prefix."training (  `tr_topic`,`tr_date`,`tr_time` , `tr_timezone` , `tr_copname` , `tr_registerlink` , `tr_name` , `tr_email` , `tr_skype` , `tr_phone` ,`tr_address`, `tr_location` , `tr_description` ,`tr_showtop`, `tr_regtime` ) 
   VALUES (
	'".$topic."','".$date."', '".$time."', '".$timezone."', '".$companyname."', '".$registerlink."', '".$name."', '".$email."','".$skypeid."', '".$phone."', '".$address."', '".$location."','".$description."', '".$showtop."', '".time()."'
	);";
    $DB->query($sql);

    redirect("./training.php?action=list","<font color=red>Added Successfully!</font>");
}


if ($_POST[action]=="update"){

	$topic          = htmlspecialchars(trim($_POST[topic]));
	$date           = htmlspecialchars(trim($_POST[date]));
	$time           = htmlspecialchars(trim($_POST['time']));
	$timezone       = htmlspecialchars(trim($_POST[timezone]));
	$email          = htmlspecialchars(trim($_POST[email]));
	$skypeid        = htmlspecialchars(trim($_POST[skypeid]));
	$name           = htmlspecialchars(trim($_POST[name]));
	$companyname    = htmlspecialchars(trim($_POST[companyname]));
	$phone          = htmlspecialchars(trim($_POST[phone]));
	$registerlink   = htmlspecialchars(trim($_POST[registerlink]));
	$address        = htmlspecialchars(trim($_POST[address]));
	$location       = htmlspecialchars(trim($_POST[location]));
	$description    = ucfirst(htmlspecialchars(trim($_POST[description])));
	$showtop        = trim($_POST['showtop']);
    
    $sql = "UPDATE ".$db_prefix."training 
	                 SET 
					`tr_topic`='".$topic."',
					`tr_date` ='".$date."',
					`tr_time`='".$time."',
					`tr_timezone`='".$timezone."',
					`tr_email`='".$email."' , 
					`tr_skype`='".$skypeid."' ,
				    `tr_name`='".$name."',
					`tr_copname`='".$companyname."' ,
					`tr_phone`='".$phone."' ,
					`tr_registerlink`='".$registerlink."' , 
					`tr_address`='".$address."',
					`tr_location`='".$location."',
					`tr_description`='".$description."',
				    `tr_showtop`='".$showtop."'
					WHERE tr_id= '".$_POST[id]."'";

	$DB->query($sql);

    redirect("./training.php?action=list&offset=".$offset,"<font color=red>Modified Successfully!</font>");
}
cpfooter();
?>